﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Caching;

namespace Fuse8.ContentManagementFramework.SystemFacade.Globalization
{
    public abstract class GetTextFacadeBase : IGetTextFacade
    {
        private const string CacheName = "gettext_res";
        private readonly ObjectCache _cache = new MemoryCache(CacheName);
        public IGetTextProvider WithDomain(string name, CultureInfo locale)
        {
            string key = string.Format("{0}_{1}", name, locale.Name);
            if (_cache.Contains(key))
            {
                return _cache[key] as IGetTextProvider;
            }
            
            var provider = GetForDomain(name, locale);
            _cache.Add(key, provider, new CacheItemPolicy() {AbsoluteExpiration = DateTimeOffset.Now.AddHours(1)});
            return provider;
        }

        protected abstract IGetTextProvider GetForDomain(string name, CultureInfo locale);
    }
}